/*
 * Copyright (C) 2014 Nathan Templon
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package unit;

import java.util.Collection;
import java.util.TreeMap;
import log.Logger;
import unit.DerivedUnit.UnitComponent;

/**
 *
 * @author Nathan Templon
 */


public class BaseUnit implements Unit, Comparable {
    public BaseUnit( UnitType type, String name ) {
        logger = Logger.getDefaultLogger();
        this.type = type;
        this.name = name;
        
        components = new UnitComponent[1];
        components[0] = new UnitComponent(this, 1);
        
        powerMap = new TreeMap<>();
        powerMap.put(type, 1);
    }
    
    
    @Override
    public double scaleToBase() {
        return 1.0;
    }
    
    
    @Override
    public double scale() {
        return 1.0;
    }
    
    
    @Override
    public double scaleTo(Unit other) {
        if(isSimilar(other)) {
            return scaleToBase() / other.scaleToBase();
        }
        else {
            return 0;
        }
    }
    
    
    @Override
    public UnitComponent[] components() {
        return components;
    }
    
    
    public UnitType type() {
        return type;
    }
    
    
    public String name() {
        return name;
    }
    
    
    @Override
    public boolean isSimilar(Unit other) {
        Collection<UnitType> otherTypes = other.powerMap().keySet();
        if (!otherTypes.equals(powerMap.keySet())) {
            return false;
        }
        for(UnitType type : otherTypes) {
            if(!other.powerMap().get(type).equals(powerMap.get(type))) {
                return false;
            }
        }
        
        return true;
    }
    
    
    @Override
    public TreeMap<UnitType, Integer> powerMap() {
        return powerMap;
    }
    
    
    @Override
    public Unit times(Unit other) {
        TreeMap<Unit, Integer> compMap = new TreeMap<>();
        TreeMap<Unit, Integer> otherCompMap = new TreeMap<>();
        TreeMap<Unit, Integer> newCompMap = new TreeMap<>();
        
        UnitComponent[] otherComps = other.components();
        
        for(UnitComponent comp : components) {
            compMap.put(comp.unit(), comp.pow());
        }
        for(UnitComponent otherComp : otherComps) {
            otherCompMap.put(otherComp.unit(), otherComp.pow());
        }
        
        for(Unit unit : compMap.keySet()) {
            if (otherCompMap.containsKey(unit)) {
                newCompMap.put(unit, otherCompMap.get(unit) + compMap.get(unit));
            }
            else {
                newCompMap.put(unit, compMap.get(unit));
            }
        }
        
        for (Unit unit : otherCompMap.keySet()) {
            if (!newCompMap.containsKey(unit)) {
                newCompMap.put(unit, otherCompMap.get(unit));
            }
        }
        
        double scale = this.scale() * other.scale();
        
        UnitComponent[] newComps = new UnitComponent[newCompMap.keySet().size()];
        int index = 0;
        for(Unit unit : newCompMap.keySet()) {
            newComps[index] = new UnitComponent(unit, newCompMap.get(unit));
            index++;
        }
        
        return new DerivedUnit(scale, newComps);
    }
    
    
    @Override
    public Unit dividedBy(Unit other) {
        TreeMap<Unit, Integer> compMap = new TreeMap<>();
        TreeMap<Unit, Integer> otherCompMap = new TreeMap<>();
        TreeMap<Unit, Integer> newCompMap = new TreeMap<>();
        
        UnitComponent[] otherComps = other.components();
        
        for(UnitComponent comp : components) {
            compMap.put(comp.unit(), comp.pow());
        }
        for(UnitComponent otherComp : otherComps) {
            otherCompMap.put(otherComp.unit(), otherComp.pow());
        }
        
        for(Unit unit : compMap.keySet()) {
            if (otherCompMap.containsKey(unit)) {
                newCompMap.put(unit, (-1 * otherCompMap.get(unit) ) + compMap.get(unit));
            }
            else {
                newCompMap.put(unit, compMap.get(unit));
            }
        }
        
        for (Unit unit : otherCompMap.keySet()) {
            if (!newCompMap.containsKey(unit)) {
                newCompMap.put(unit, -1 * otherCompMap.get(unit));
            }
        }
        
        double scale = this.scale() / other.scale();
        
        UnitComponent[] newComps = new UnitComponent[newCompMap.keySet().size()];
        int index = 0;
        for(Unit unit : newCompMap.keySet()) {
            newComps[index] = new UnitComponent(unit, newCompMap.get(unit));
            index++;
        }
        
        return new DerivedUnit(scale, newComps);
    }
    
    
    @Override
    public Unit compressToBase() {
        return this;
    }
    
    
    @Override
    public Unit scaleless() {
        return this;
    }
    
    
    @Override
    public String toString() {
        return name;
    }
    
    
    @Override
    public int compareTo(Object o) {
        // The position that we are relative to the provided object
        final int BEFORE = -1;
        final int EQUAL = 0;
        final int AFTER = 1;
        
        if (this.equals(o)) {
            return EQUAL;
        }
        
        if (o instanceof Unit) {
            Unit newO = (Unit)o;
            return this.name().compareTo(newO.name());
        }
        
        // If they didn't send us a UnitType, there isn't much else we can do
        return EQUAL;
    }
    
    
    private final UnitType type;
    private final String name;
    private final TreeMap<UnitType, Integer> powerMap;
    private UnitComponent[] components;
    
    private Logger logger;

}
